﻿namespace ClassLibrary.DataLayer.DataObjects
{
    using ClassLibrary.BusinessLayer.BusinessObjects;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;

    public class CountriesDAO : ICountriesDAO
    {
        public bool DeleteCountries(Countries clsCountries)
        {
            return clsCountries.Save("D");
        }

        public IList<Countries> GetCountries(Countries clsCountries)
        {
            DataTable dt = Db.GetDataTable(clsCountries.CreateGetCommandParameters());
            clsCountries.LoadDataToCollection(dt.Rows[0]);
            if (dt.Rows[0] == null)
            {
                throw new Exception("Countries doesn't exist");
            }
            IList<Countries> list = new List<Countries>();
            list.Add(clsCountries);
            return list;
        }

        public IList<Countries> GetCountries(string countries)
        {
            Countries clsCountries = new Countries();
            clsCountries._Country = countries;
            return this.GetCountries(clsCountries);
        }

        public IList<Countries> GetsCountries()
        {
            Countries clsCountries = new Countries();
            IDataReader dtReader = Db.GetDataReader(clsCountries.CreateGetsCommandParameters());
            IList<Countries> list = new List<Countries>();
            while (dtReader.Read())
            {
                clsCountries = new Countries();
                clsCountries.LoadDataToCollection(dtReader);
                list.Add(clsCountries);
            }
            return list;
        }

        public DataTable GetsCountries(string SPName, DbParameter[] param)
        {
            Countries clsCountries = new Countries();
            return Db.GetDataTable(clsCountries.GetsDataReader(SPName, param));
        }

        public bool InsertCountries(Countries clsCountries)
        {
            return clsCountries.Save("I");
        }

        public int InsertCountriesReturnIdentity(Countries clsCountries)
        {
            return clsCountries.Save("I", true);
        }

        public string InsertCountriesReturnOutput(Countries clsCountries)
        {
            return clsCountries.Save("I", "@CountriesID");
        }

        public IList<Countries> ListCountries(Countries clsCountries)
        {
            IDataReader dtReader = Db.GetDataReader(clsCountries.CreateListCommandParameters());
            IList<Countries> list = new List<Countries>();
            while (dtReader.Read())
            {
                Countries clsCountriess = new Countries();
                clsCountriess.LoadDataToCollection(dtReader);
                list.Add(clsCountriess);
            }
            return list;
        }

        public IList<Countries> ListCountries(string countries)
        {
            Countries clsCountries = new Countries();
            clsCountries._Country = countries;
            return this.ListCountries(clsCountries);
        }

        public bool UpdateCountries(Countries clsCountries)
        {
            return clsCountries.Save("U");
        }

        public int UpdateCountries(string SPName, DbParameter[] param)
        {
            Countries clsCountries = new Countries();
            return Db.ExecuteNonQuery(clsCountries.UpdateQuery(SPName, param));
        }
    }
}

